/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class Main {

    //合并两个有序链表
    public static Node mergeTwoLists(Node headA, Node headB) {
        Node newHead = new Node(-1);

        if(headA == null && headB == null){
            return null;
        }

        Node cur = newHead;

        while(headA != null && headB != null){
            if(headA.val < headB.val){
                if(cur == null){
                    cur = headA;
                }else{
                    cur.next = headA;
                    cur = cur.next;
                }
                headA = headA.next;

            }else{
                if(cur == null){
                    cur = headB;
                }else{
                    cur.next = headB;
                    cur = cur.next;
                }
                headB = headB.next;
            }
        }

        if(headA == null){
            cur.next = headB;
        }
        if(headB == null){
            cur.next = headA;
        }

        return newHead.next;

    }

    //相交链表
    public static Node getIntersectionNode(Node headA,Node headB) {
        if(headA == null || headB == null){
            return null;
        }

        Node curA =headA;
        Node curB =headB;
        int lenA = 1;
        int lenB = 1;
        while(curA != null){
            lenA++;
            curA = curA.next;
        }
        while(curB != null){
            lenB++;
            curB = curB.next;
        }
        curA =  headA;
        curB =  headB;
        int len = lenA - lenB;
        if(len < 0){
            len = lenB - lenA;
            while(len != 0){
                curB = curB.next;
                len--;
            }
        }else{
            while(len != 0){
                curA = curA.next;
                len--;
            }
        }
        while(curA != curB){
            curA = curA.next;
            curB = curB.next;
        }
        if(curA == curB){
            return curA;
        }else{
            return null;
        }
    }

    public static void creatIntersection(Node headA,Node headB){
        headA.next.next = headB.next.next;
    }

    public static void creatCycle(Node head){
        Node cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = head.next.next;
    }

    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.addLast(1);
        linkedList.addLast(2);
        linkedList.addLast(3);
        linkedList.addLast(4);
        linkedList.addLast(5);
//        linkedList.addLast(4);
//        linkedList.addLast(4);
//        linkedList.addLast(5);
        linkedList.display2(linkedList.head);

//        LinkedList linkedList1 = new LinkedList();
//        linkedList1.addLast(13);
//        linkedList1.addLast(25);
//        linkedList1.addLast(30);
//        linkedList.display2(linkedList1.head);
//        linkedList.creatList();
//        linkedList.display();
//        int listsize = linkedList.size();
//        System.out.println(listsize);
//        linkedList.display();
//        System.out.println(linkedList.contain(100));
//        linkedList.addLast(12);
//        linkedList.addLast(23);
//        linkedList.addLast(34);
//        linkedList.addLast(45);
//        linkedList.addLast(56);
//        linkedList.display();
//        linkedList.addIndex(0,99);
//        linkedList.display();
//        linkedList.remove(23);
//        linkedList.removeAllKey(23);
//        linkedList.display();
//        Node ret = linkedList.reverseList();
//        linkedList.display2(ret);
//        linkedList.clean();
//        linkedList.display();
//        Node ret = linkedList.middleNode();
//        System.out.println(ret.val);
//          Node ret = mergeTwoLists(linkedList.head,linkedList1.head);
//          linkedList.display2(ret);
//        Node ret = linkedList.partition(20);
//        linkedList.display2(ret);
//        Node ret = linkedList.deleteDuplication();
//        linkedList.display2(ret);
//        boolean ret = linkedList.chkPalindrome();
//        System.out.println(ret);
//        creatIntersection(linkedList.head,linkedList1.head);
//        Node ret = getIntersectionNode(linkedList.head,linkedList1.head);
//        System.out.println(ret.val);
//        System.out.println(linkedList.hasCycle());
//        creatCycle(linkedList.head);
//        Node ret = linkedList.detectCycle();
//        System.out.println(ret.val);        linkedList.swapPairs();
//        linkedList.display();
        linkedList.swapNodes(2);
        linkedList.display();

    }


}

